package com.rafahead.bloquinho.dao;



import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.rafahead.bloquinho.model.Player;
import com.rafahead.bloquinho.util.BasicoDAO;
import com.rafahead.bloquinho.util.TableBuilder;
import com.rafahead.bloquinho.util.Utils;

public class PlayerDAO extends BasicoDAO {

	public PlayerDAO(Context ctx) {
		super(ctx);
	}

	public static final String TABLE_NAME = "PLAYER";

	public static final String COLUMN_ID = "_id";
	public static final String COLUMN_NAME = "NAME";
	public static final String COLUMN_CANCEL = "CANCEL";
	public static final String[] COLUMNS = {COLUMN_ID,COLUMN_NAME};
	
	private static String defineTable() {
		TableBuilder tb = new TableBuilder(TABLE_NAME);
		try {
			tb.setPrimaryKey(COLUMN_ID, TableBuilder.INTEGER, true);
			tb.addColuna(COLUMN_NAME, TableBuilder.TEXT, true);
			tb.addColuna(COLUMN_CANCEL, TableBuilder.INTEGER, true);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return tb.toString();
	}

	/**
	 * 
	 */
	public static final String TABLE_CREATE = defineTable();

	public static void createDummyData(SQLiteDatabase db) {

		ContentValues values = new ContentValues();
		
		values.put(COLUMN_NAME, "Rafael");
		values.put(COLUMN_CANCEL, false);
		db.insert(TABLE_NAME, null, values);
		
		values.clear();
		values.put(COLUMN_NAME, "Luisa");
		values.put(COLUMN_CANCEL, false);
		db.insert(TABLE_NAME, null, values);
		
		values.clear();
		values.put(COLUMN_NAME, "Sunção");
		values.put(COLUMN_CANCEL, false);
		db.insert(TABLE_NAME, null, values);
		
		values.clear();
		values.put(COLUMN_NAME, "Martinha");
		values.put(COLUMN_CANCEL, false);
		db.insert(TABLE_NAME, null, values);
		

	}

	public long insertPlayer(String name) {

		ContentValues values = playerToContentValues(new Player(name));
 
		return inserir(TABLE_NAME, values);
	}

	public static ContentValues playerToContentValues(Player categoria) {
		ContentValues values = new ContentValues();
		values.put(COLUMN_NAME, categoria.getName());
		values.put(COLUMN_CANCEL, Utils.booleanToInteger(categoria.isCancel()));
		values.put(COLUMN_ID, categoria.getId());
		return values;
	}

	public boolean removePlayer(long id) {
		ContentValues values = new ContentValues();
		values.put(COLUMN_CANCEL,Utils.booleanToInteger(true));
		Long result = atualizar(TABLE_NAME, values, new String[] { COLUMN_ID },new String[] { String.valueOf(id) });
		if (result!=null){
			return true;
		}
		return false;
	}

	public Cursor searchAllPlayers() {

		Log.w("PlayerDAO", "Consultando tabela " + TABLE_NAME);
		
	/*	Cursor mCursor = mDb.query(false, TABLE_NAME, COLUMNS, COLUMN_CANCEL + "='" + Utils.booleanToInteger(true) + "'", null, null,null, null, null);

		if (mCursor != null) {
			mCursor.moveToFirst();
		}

		return mCursor;	
*/
		
		return super.consultaBasica(TABLE_NAME, COLUMNS, COLUMN_CANCEL + "='" + Utils.booleanToInteger(false) + "'");
	}

	public Player searchPlayer(long id) throws SQLException {
		Player player = null;
		Cursor cursor = consultar(TABLE_NAME, null, COLUMN_ID,String.valueOf(id));
		
		if (cursor!=null && cursor.getCount()>0){
			player = new Player();
			player.setName(cursor.getString(cursor.getColumnIndexOrThrow(PlayerDAO.COLUMN_NAME)));
			player.setId(cursor.getLong(cursor.getColumnIndexOrThrow(PlayerDAO.COLUMN_ID)));
			player.setCancel(Utils.integerToBoolean(cursor.getInt(cursor.getColumnIndexOrThrow(PlayerDAO.COLUMN_CANCEL))));
		}
		return player;
	}

	public long updatePlayer(long id, String name) {
		ContentValues values = new ContentValues();
		values.put(COLUMN_NAME, name);
		return atualizar(TABLE_NAME, values, new String[] { COLUMN_ID },new String[] { String.valueOf(id) });
	}

	public Cursor searchPlayerLike(String name) {
		return consultarLike(TABLE_NAME, null, COLUMN_NAME,name);
	}
}
